home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cuj9205.zip / 1005022A < prev    next >
Text File  |  1992-06-02  |  6KB  |  224 lines

  1.  
  2. /* --------------------------------------------------------------
  3.  
  4. This program creates and manages a queue of strings stored as a 2D
  5. array of chars.  Each row of this array represents an entry in the
  6. queue.  The user can either add new entries to the rear of the queue
  7. or remove old entries from the front.
  8.  
  9. -------------------------------------------------------------- */
  10.  
  11. #include <stdio.h>
  12. #include <ctype.h>
  13.  
  14. #define MAX_NODES 6    /* max number of nodes in array */
  15. #define MAX_LEN 10    /* max length of string in entry */
  16. #define QNULL 0        /* null array index value */
  17.  
  18. #define ADD_NODE 'A'
  19. #define EXIT 'E'
  20. #define HELP '?'
  21. #define REMOVE_NODE 'D'
  22. #define LIST_QUEUE 'L'
  23. #define DUMP_QUEUE 'M'
  24.  
  25. char queue[MAX_NODES + 1][MAX_LEN + 1];
  26.  
  27. unsigned int head_node = QNULL;    /* start of queue */
  28. unsigned int tail_node = QNULL;    /* end of queue */
  29.  
  30. /* ----------------------------------------------------------- */
  31.  
  32. main()
  33. {
  34.     unsigned int i, node, node_number;
  35.     int inchar = 'x';    /* force initial prompt */
  36.     int done = 0;
  37.  
  38. /* ----------------------------------------------------------- */
  39.  
  40.     while (!done) {
  41.         if (inchar != ' ')
  42.             printf("\nEnter Action Code (%c for help): ", HELP);
  43.         switch(toupper(inchar = getchar())) }
  44.  
  45. /* ----------------------------------------------------------- */
  46.  
  47.     case EXIT:
  48.         done = 1;
  49.         break;
  50.  
  51. /* ----------------------------------------------------------- */
  52.  
  53.     default:
  54.         printf("\n   Invalid command. Please try again\n");
  55.         break;
  56.  
  57. /* ----------------------------------------------------------- */
  58.  
  59.     case HELP:
  60.         printf("\n   The action codes are:\n");
  61.         printf("\t%c - Produces this help message\n", HELP);
  62.         printf("\t%c - Exit this program\n", EXIT);
  63.         printf("\t%c - Add a new node to the tail\n", ADD_NODE);
  64.         printf("\t%c - Remove head node\n", REMOVE_NODE);
  65.         printf("\t%c - List all entries in the queue\n", LIST_QUEUE);
  66.         printf("\t%c - List contents of queue array\n", DUMP_QUEUE);
  67.         break;
  68.  
  69. /* ----------------------------------------------------------- */
  70.  
  71.     case '\n':    /* ignore white space on input */
  72.     case ' ' :
  73.     case '\t':
  74.     case '\v':
  75.     case '\f':
  76.         inchar = ' ';    /* indicate white space input */
  77.         break;
  78.  
  79. /* --------------------------------------------------------------
  80.  
  81. ADD_NODE: The steps to add a new node to the end of the queue are:
  82.  
  83. A.  If queue full, complain and get out. This can happen in one of two 
  84.     ways: array element 1 is the head of the queue and array element
  85.     MAX_NODES represent the tail or; the queue is wrapped in a 
  86.     circular manner and exactly fills the array.
  87.  
  88. B.  If this is the first node, make it the head and tail. Otherwise, 
  89.     if the current tail is the last element, wrap to element 1. 
  90.     Otherwise, go to the next element beyond the current tail.
  91.  
  92. C.  Have a new node so fill it with data.
  93.  
  94. -------------------------------------------------------------- */
  95.  
  96.     case ADD_NODE:
  97.  
  98. /*A*/        if ((head_node == 1 && tail_node == MAX_NODES) ||
  99.             head_node == tail_node + 1) {
  100.             printf("\n   Queue is full\n");
  101.             break;
  102.         }
  103.  
  104. /*B*/        if (head_node == QNULL) {
  105.             head_node = tail_node = 1;
  106.         }    
  107.         else if (tail_node == MAX_NODES) {
  108.             tail_node = 1;
  109.         }
  110.         else {
  111.             ++tail_node;
  112.         }
  113.  
  114. /*C*/        printf("\n   Enter new node's value: ");
  115.         scanf("%10s", queue[tail_node]);
  116.         printf("\n   Node added\n");
  117.         break;
  118.  
  119. /* --------------------------------------------------------------
  120.  
  121. REMOVE_NODE: The steps to remove the tail node are:
  122.  
  123. A.  Complain if list empty.
  124.  
  125. B.  Adjust the head and tail nodes as follows: If this is the only 
  126.     node in the queue, make the head and tail both null. Otherwise, if 
  127.     this node is stored in the last array element, wrap the new head 
  128.     to element 1. Otherwise, make the new head one more than the old 
  129.     one.
  130.  
  131. -------------------------------------------------------------- */
  132.  
  133.     case REMOVE_NODE:
  134.  
  135. /*A*/        if (head_node == QNULL) {
  136.             printf("\n   Queue is empty\n");
  137.             break;
  138.         }
  139.  
  140. /*B*/           queue[head_node][0] = '\0';    /* overwrite string */
  141.         if (head_node == tail_node) {
  142.             head_node = tail_node = QNULL;
  143.         }
  144.         else if (head_node == MAX_NODES) {
  145.             head_node = 1;
  146.         }
  147.         else {
  148.             ++head_node;
  149.         }
  150.         printf("\n   Node removed\n");
  151.         break;
  152.  
  153. /* --------------------------------------------------------------
  154.  
  155. LIST_QUEUE: The steps to display all nodes in the queue are:
  156.  
  157. A.  Complain if queue empty.
  158.  
  159. B.  Traverse queue starting at the head node displaying each node's
  160.     string and the logical node number. Need to wrap if queue is 
  161.     stored in a circular manner.
  162.  
  163. -------------------------------------------------------------- */
  164.  
  165.     case LIST_QUEUE:
  166.  
  167. /*A*/        if (head_node == QNULL) {
  168.             printf("\n   Queue is empty\n");
  169.             break;
  170.         }
  171.  
  172. /*B*/        printf("\n   Queue nodes are as follows:\n");
  173.         node = head_node;
  174.         node_number = 0;
  175.         while (1) {
  176.             printf("\tNode %u => %s\n", ++node_number, queue[node]);
  177.             if (node == tail_node)
  178.                  break;
  179.  
  180.             if (node == MAX_NODES) {
  181.                  node = 1;
  182.              }
  183.             else {
  184.                 ++node;
  185.             }
  186.         }
  187.  
  188.         break;
  189.  
  190. /* --------------------------------------------------------------
  191.  
  192. DUMP_QUEUE: The steps to dump all `nodes' in the queue array are:
  193.  
  194. A.  Traverse queue array starting at [1] displaying the array element
  195.     number and corresponding string.  No need to wrap as this is a
  196.     physical dump, not a logical one.
  197.  
  198. -------------------------------------------------------------- */
  199.  
  200.     case DUMP_QUEUE:
  201.  
  202. /*A*/        printf("\n   [%d] is head_node, [%d] is tail_node\n",
  203.             head_node, tail_node);
  204.         if (head_node == QNULL) {
  205.             printf("\n   Queue is empty\n");
  206.         }
  207.  
  208.         printf("\n   Queue array nodes are as follows:\n");
  209.         for (i = 1; i <= MAX_NODES; ++i) {
  210.             printf("\t[%u] => %s\n", i, queue[i]);
  211.         }
  212.  
  213.         break;
  214.  
  215. /* ----------------------------------------------------------- */
  216.  
  217.         }
  218.     }
  219.  
  220.     return (0);
  221. }
  222. /* ----------------------------------------------------------- */
  223.  
  224.